package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Blocks;

public class GenericBlockDAO extends AutoKeys implements
		org.jetnuke.dao.BlockDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBlockDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#retrieveByPK(int)
	 */
	public Blocks retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Blocks s = new Blocks();

		if (rs.next()) {
			fillBlockFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillBlockFromResultSet(Blocks s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillBlockFromResultSet(Blocks, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setBID(rs.getInt("BID"));
		s.setBKEY(rs.getString("BKEY"));
		s.setCONTENT(rs.getString("CONTENT"));
		s.setPATH(rs.getString("PATH"));
		s.setPOSITION(rs.getString("POSITION"));
		s.setREFRESH(rs.getInt("REFRESH"));
		s.setRSS_URL(rs.getString("RSS_URL"));
		s.setTIME(rs.getString("TIME1"));
		s.setTITLE(rs.getString("TITLE"));
		s.setWEIGHT(rs.getInt("WEIGHT"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillBlockFromResultSet(Blocks, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Blocks u = new Blocks();
			fillBlockFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#blockActivate(int)
	 */
	public void blockActivate(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("blockActivate(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.activate"));
		p.setInt(1, bid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("blockActivate(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#blockDeactivate(int)
	 */
	public void blockDeactivate(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("blockDeactivate(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.deactivate"));
		p.setInt(1, bid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("blockDeactivate(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#update(org.jetnuke.entities.Blocks)
	 */
	public void update(Blocks blk) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Blocks) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.update"));
		p.setInt(1, blk.getACTIVE());
		p.setString(2, blk.getBKEY());
		p.setString(3, blk.getCONTENT());
		p.setString(4, blk.getPATH());
		p.setString(5, blk.getPOSITION());
		p.setInt(6, blk.getREFRESH());
		p.setString(7, blk.getRSS_URL());
		p.setString(8, blk.getTIME());
		p.setString(9, blk.getTITLE());
		p.setInt(10, blk.getWEIGHT());
		p.setInt(11, blk.getBID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Blocks) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#selectAllByWeight()
	 */
	public List selectAllByWeight() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllByWeight() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlockModel.selectAllByWeight"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllByWeight() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#addNew(org.jetnuke.entities.Blocks)
	 */
	public int addNew(Blocks block) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Blocks) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this.getStatementForAutoKeys("BlockModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BlockModel.lastGeneratedBlockId"));
		initNewBlock(block, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Blocks) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param block
	 * @param p
	 */
	protected void initNewBlock(Blocks block, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewBlock(Blocks, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, block.getACTIVE());
		p.setString(2, block.getBKEY());
		p.setString(3, block.getCONTENT());
		p.setString(4, block.getPATH());
		p.setString(5, block.getPOSITION());
		p.setInt(6, block.getREFRESH());
		p.setString(7, block.getRSS_URL());
		p.setString(8, block.getTIME());
		p.setString(9, block.getTITLE());
		p.setInt(10, block.getWEIGHT());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewBlock(Blocks, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.BlockModel#deleteByPK(int)
	 */
	public void deleteByPK(int bid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlockModel.deleteByPK"));
		p.setInt(1, bid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

}
